package com.watermeter.android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.oa.android.R;
import com.watermeter.android.entry.DownRoute;
import com.watermeter.android.entry.curstorInfo;
import com.watermeter.android.utils.Util;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * 修改信息
 *
 * @author -zouqh
 */
public class ChangeInfoActivity extends Activity implements OnClickListener {

    private ImageView close;
    private TextView title;
    private TextView save, states;
    private EditText kfuser, sbbh, phone, address, cbxh, bzxx;
    private int postion;
    private DownRoute downRoute;
    private int allToal;
    private LinearLayout layout;
    public DbManager.DaoConfig daoConfig;
    private List<String> statesList;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        postion = Integer.parseInt(getIntent().getStringExtra("id"));
        setContentView(R.layout.change_info_activity);
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        save = (TextView) findViewById(R.id.ritgh_text);
        states = (TextView) findViewById(R.id.states);
        kfuser = (EditText) findViewById(R.id.kfuser);
        sbbh = (EditText) findViewById(R.id.sbbh);
        phone = (EditText) findViewById(R.id.phone);
        address = (EditText) findViewById(R.id.address);
        cbxh = (EditText) findViewById(R.id.cbxh);
        address = (EditText) findViewById(R.id.address);
        bzxx = (EditText) findViewById(R.id.bzxx);
        save.setText("保存");
        title.setText("修改信息");
        save.setVisibility(View.VISIBLE);
        close.setOnClickListener(this);
        save.setOnClickListener(this);
        states.setOnClickListener(this);
        allToal = RouteItemActivity.activity.downRoutelist1.size();
        setValue();
        statesList = new ArrayList<>();
        statesList.add("正常");
        statesList.add("停水");
        statesList.add("销户");
        statesList.add("停收");
        statesList.add("停用");
        statesList.add("报装");
        statesList.add("新户");


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:
                String d = kfuser.getText().toString().trim();
                if (d.length() > 0) {
                    UpRoute();
                } else {
                    Toast.makeText(this, "请填写客户姓名!", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.states:
                showState();
                break;

            default:
                break;
        }
    }

    /**
     * 设置值
     */
    private void setValue() {

        downRoute = RouteItemActivity.activity.downRoutelist1.get(postion);
        if (downRoute != null) {
            int a = downRoute.getSBZD();
            int b = Integer.parseInt(downRoute.getSBQD());
            kfuser.setText(downRoute.getUSERNAME());
            sbbh.setText("" + downRoute.getSBBH());
            phone.setText("" + downRoute.getLXDH());
            address.setText(downRoute.getXXDZ());
            cbxh.setText( downRoute.getCBXH());
            bzxx.setText(downRoute.getBZXX());
            states.setText(downRoute.getSBZT() + "");


        }
    }


    /**
     * 更新
     */
    private void UpRoute() {

        int b=0;
        DbManager manager = x.getDb(daoConfig);
        try {
            //创建对象，用来生成表
//            downRoute
            String str1 = kfuser.getText().toString();
            String str2 = sbbh.getText().toString();
            String str3 = phone.getText().toString();
            String str4 = address.getText().toString();
            String str5 = cbxh.getText().toString();
            String str6 = states.getText().toString();
            String str7 = bzxx.getText().toString();
            curstorInfo curstorInfo=new curstorInfo();

            if (Util.NoNull(downRoute.getUSERNAME())&&!downRoute.getUSERNAME().equals(str1)){

                curstorInfo.setNUSERNAME(downRoute.getUSERNAME());
                downRoute.setUSERNAME(str1);
                RouteItemActivity.activity.downRoutelist1.get(postion).setUSERNAME(str1);
                b++;
            }
            if (!downRoute.getSBBH().equals(str2)){

                curstorInfo.setNSBBH(downRoute.getSBBH());
                downRoute.setSBBH(str2);
                RouteItemActivity.activity.downRoutelist1.get(postion).setSBBH(str2);
                b++;
            }
            if(!downRoute.getLXDH().equals(str3)){

                curstorInfo.setNLXDH(downRoute.getLXDH());
                downRoute.setLXDH(str3);
                RouteItemActivity.activity.downRoutelist1.get(postion).setLXDH(str3);
                b++;
            }

            if (!downRoute.getXXDZ().equals(str4))
            {

                curstorInfo.setNXXDZ(downRoute.getXXDZ());
                downRoute.setXXDZ(str4);
                RouteItemActivity.activity.downRoutelist1.get(postion).setXXDZ(str4);
                b++;
            }

            if (!downRoute.getCBXH().equals(str5))
            {

                curstorInfo.setNCBXH(downRoute.getCBXH());
                downRoute.setCBXH(str5);
                RouteItemActivity.activity.downRoutelist1.get(postion).setCBXH(str5);
                b++;
            }

            if (!downRoute.getSBZT().equals(str6))
            {

                curstorInfo.setNSBZT(downRoute.getSBZT());
                downRoute.setSBZT(str6);
                RouteItemActivity.activity.downRoutelist1.get(postion).setSBZT(str6);
                b++;
            }

            if (!downRoute.getBZXX().equals(str7))
            {

                curstorInfo.setNBZXX(downRoute.getBZXX());
                downRoute.setBZXX(str7);
                RouteItemActivity.activity.downRoutelist1.get(postion).setBZXX(str7);
                b++;
            }

            if (b<=0){
                Toast.makeText(getApplicationContext(),"没有信心修改！",10).show();
                return;
            }




            curstorInfo.setUSERNAME(downRoute.getUSERNAME());
            curstorInfo.setSBBH(downRoute.getSBBH());
            curstorInfo.setLXDH(downRoute.getLXDH());
            curstorInfo.setXXDZ(downRoute.getXXDZ());
            curstorInfo.setCBXH(downRoute.getCBXH());
            curstorInfo.setSBZT(downRoute.getSBZT());
            curstorInfo.setBZXX(downRoute.getBZXX());
            curstorInfo.setId(downRoute.getId());
            curstorInfo.setUSERID(downRoute.getUSERID());
            manager.saveOrUpdate(downRoute);
            System.out.println("curstorInfo==="+curstorInfo);
            RouteItemActivity.activity.flag = 1;
            List<curstorInfo> u = manager.selector(curstorInfo.class).where("id","=",downRoute.getId()).findAll();
            if(u!=null&&u.size()>0){
                Util.printLog("ChangeInfoActivity-------",u.size()+"");
                for (int i=0;i<u.size();i++){
                    curstorInfo.setInfoId(u.get(i).getInfoId());
                    manager.saveOrUpdate(curstorInfo);
                }
            }else{
                manager.saveOrUpdate(curstorInfo);
            }
            Toast.makeText(this, "保存成功！", Toast.LENGTH_SHORT).show();

        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    /**
     * 选择状态
     */
    private void showState() {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.sb_state_dailog,
                null);
        // 对话框
        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.show();

        dialog.setContentView(layout);
        ListView listView = (ListView) layout.findViewById(R.id.statesListView);
        listView.setAdapter(new statesBase());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                states.setText(statesList.get(position));
                dialog.dismiss();
            }
        });

    }


    class statesBase extends BaseAdapter{

        @Override
        public int getCount() {
            return statesList.size();
        }

        @Override
        public Object getItem(int position) {
            return statesList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;
            if(convertView==null){
                convertView=LayoutInflater.from(ChangeInfoActivity.this).inflate(R.layout.text_item,null);
                textView=(TextView)convertView.findViewById(R.id.name);
                convertView.setTag(textView);
            }else{
                textView= (TextView) convertView.getTag();
            }
            textView.setText(statesList.get(position));
            return convertView;
        }
    }




}
